package org.focusj.scala.colls

//测试Iterable的常用方法
object HowToUseColl extends App {
  val iSeq = Seq[Int](1, 2, 3, 4, 5, 6, 7, 8, 9)
  val mSeq = scala.collection.mutable.Seq(1, 2, 3, 4, 5, 6, 7, 8)
  /*****************************************获得首、尾元素*****************************************/
  iSeq.head
  iSeq.headOption
  iSeq.last
  iSeq.lastOption

  //获得除去头元素，尾元素的剩余集合
  //println(iSeq.init)
  //println(iSeq.tail)

  /*****************************************应用到所有集合的操作*****************************************/
  /*****************************************map foreach flatMap collect*****************************************/
  val mapR = iSeq.map { x => /*注意map是可以有返回值的*/
    0 to x
  }
  //println(mapR)
  iSeq.foreach { x => /*foreach只是对每一个元素做出相应的操作，没有返回值*/
    x * x
  }
  val flR = iSeq.flatMap { x => /*请注意看这里比较map和flatMap的区别*/
    0 to x
  }
  //println(flR)

  val cR = iSeq.collect {
    case x if x > 0 => x * x
    case x if x < 0 => x * x
  }
  //println(cR)

  /*****************************************reduceLeft reduceRight foldLeft foldRight*****************************************/
  val rlR = iSeq.reduceLeft((r, e) => { r + e })
  val rrR = iSeq.reduceRight((e, r) => { r + e })
  //println(rlR)
  //println(rrR)

  val flRR = iSeq.foldLeft(List[Int]())((r, e) => { r :+ e * e })
  val frRR = iSeq.foldRight(List[Int]())((e, r) => { r :+ e * e })
  //println(flRR)
  //println(frRR)

  /*****************************************reduce fold aggregate*****************************************/
  val agrR = iSeq.par.aggregate(0)(
    (r, e) => { r + e * e },
    (c, d) => {
      c + d
    })
  //println(agrR)
  var fR = iSeq.par.fold(0)(_ + _)
  //println(fR)
  var rR = iSeq.reduce(_ + _)
  //println(fR)
  /*****************************************sum(和) product(乘积) max min*****************************************/
  var pR = iSeq.product
  //println(pR)
  /*****************************************count forall exists*****************************************/
  //返回满足条件的计数；是不是全部满足；是否存在
  /*****************************************filter filterNot partition*****************************************/
  var ptR = iSeq.partition(_ % 2 == 0)
  var fltR = iSeq.filter(_ % 2 == 0)
  var fltnR = iSeq.filterNot(_ % 2 == 0)
  //println(ptR)
  //println(fltR)
  //println(fltnR)
  /*****************************************takeWhile dropWhile span*****************************************/
  var tkR = iSeq.takeWhile(_ % 2 == 1)
  var dpR = iSeq.dropWhile(_ % 2 == 1)
  var spR = iSeq.span(_ % 2 == 1)
  //println(tkR)
  //println(dpR)
  //println(spR)
  /******************************************take drop splitAt****************************************/
  //前n个元素，其他元素，两个组成的对偶
  /******************************************takeRight dropRight*****************************************/
  val trR = iSeq.takeRight(3)
  val drR = iSeq.dropRight(3)
  //println(trR)
  //println(drR)
  /******************************************slice *****************************************/
  val slR = iSeq.slice(0, 5)
  //println(slR)
  /******************************************zip zipAll zipWithIndex*****************************************/
  //拉练操作
  val zR = iSeq.zip(mSeq)
  val zpR = iSeq.zipWithIndex
  val zaR = iSeq.zipAll(mSeq, 0, 1)
  //println(zR)
  //println(zpR)
  //println(zaR)
  /******************************************grouped sliding*****************************************/
  //grouped:把整个列表按照分成size/n个组
  //sliding：重叠的把列表按每组n个元素进行分组
  val gpR = iSeq.grouped(2)
  gpR foreach println
  val sdR = iSeq.sliding(3)
  sdR foreach println
}